package com.origin.niuke.tree;

import general_class.TreeNode;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/5/15 15:49
 * 判断是不是平衡二叉树
 * 算法：后序遍历
 */
public class NC62 {

    public boolean IsBalanced_Solution(TreeNode root) {
        return depth(root) != -1;
    }

    int depth(TreeNode root) {
        if (root == null) return 0;
        int lDepth = depth(root.left);
        if (lDepth == -1) return -1;
        int rDepth = depth(root.right);
        if (rDepth == -1) return -1;
        if (Math.abs(lDepth - rDepth) > 1) return -1;
        return Math.max(lDepth, rDepth) + 1;
    }

}
